package com.bing.jh.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.bing.bingPro.util.ProvinceUtil;
import com.bing.jh.entity.dto.TZwArchivesDto;
import com.bing.jh.entity.vo.TZwResult;
import com.bing.jh.mapper.DzWzLevelConfigMapper;
import com.bing.jh.mapper.TZwArchivesDorisDao;
import com.bing.jh.service.TZwArchivesService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("tZwArchivesService")
public class TZwArchivesServiceImpl implements TZwArchivesService {
    @Resource
    private TZwArchivesDorisDao tZwArchivesDorisDao;
    @Resource
    private DzWzLevelConfigMapper dzWzLevelConfigMapper;

    /**
     * 分页查询
     *
     * @param tZwArchives 筛选条件
     * @return 查询结果
     */
    @Override
    public List<Map<String, Object>> queryByPage(TZwArchivesDto tZwArchives) {
        int i =1;
        tZwArchives.setIdentify(i);
        //  type = 0
        List<TZwResult> tZwArchivesAndDetailsEntities = tZwArchivesDorisDao.queryRank(tZwArchives.getMonth(),tZwArchives.getIdentify());
        if (CollUtil.isEmpty(tZwArchivesAndDetailsEntities)) {
            tZwArchivesAndDetailsEntities = tZwArchivesDorisDao.queryDefault(tZwArchives.getMonth(),tZwArchives.getIdentify());
        }

        Map<String, List<Integer>> numMap = tZwArchivesAndDetailsEntities.stream().collect(Collectors.toMap(
                TZwResult::getProvinceName,
                v -> {
                    Integer num = Integer.valueOf(v.getNum());
                    List<Integer> integers = new ArrayList<>();
                    integers.add(num);
                    return integers;
                },
                (v1, v2) -> {
                    v1.addAll(v2);
                    return v1;
                }
        ));
        List<Map<String, Object>> mapList = tZwArchivesDorisDao.queryByArchivesList(tZwArchives);

        mapList.forEach(o -> {
            String provinceName = String.valueOf(o.get("provinceName"));
            List<Integer> integers = numMap.get(provinceName);
            if (!CollectionUtils.isEmpty(integers)) {
                Integer rank = Integer.valueOf(String.valueOf(o.get("portraitScoreProvRank")));
                //判断是哪个区间
                Integer level = getLevel(rank, integers);
                //获取等级
                o.put("level", level);
            }
        });
        return mapList;

        /*Map<String, Map<String,List<Integer>>> numMap = tZwArchivesAndDetailsEntities.stream().collect(Collectors.toMap(
                TZwResult::getProvinceName,
                v -> {
                    String scoreType = v.getScoreType();
                    Map<String,List<Integer>> map = new HashMap();
                    if (scoreType.equals("0")){
                        Integer num = Integer.valueOf(v.getNum());
                        List<Integer> integers = new ArrayList<>();
                        integers.add(num);
                        map.put("0",integers);
                    }else if (scoreType.equals("1")){
                        Integer num = Integer.valueOf(v.getNum());
                        List<Integer> integers = new ArrayList<>();
                        integers.add(num);
                        map.put("1",integers);
                    }
                    return map;
                },
                (v1, v2) -> {
                    initMap("0",v1,v2);
                    initMap("1",v1,v2);
                    return v1;
                }
        ));




        List<Map<String, Object>> mapList = tZwArchivesDorisDao.queryByArchivesList(tZwArchives);

        mapList.forEach(o->{
            String provinceName = String.valueOf(o.get("provinceName"));
            Map<String, List<Integer>> stringListMap = numMap.get(provinceName);
            List<Integer> integers0 = stringListMap.get("0");
            List<Integer> integers1 = stringListMap.get("1");
            if (!CollectionUtils.isEmpty(integers0)){
                Integer rank = Integer.valueOf(String.valueOf(o.get("portraitScoreProvRank")));
                //判断是哪个区间
                Integer level = getLevel(rank, integers0);
                //获取等级
                o.put("level0",level);
            }
            if (!CollectionUtils.isEmpty(integers1)){
                Integer rank = Integer.valueOf(String.valueOf(o.get("portraitScoreProvRank")));
                //判断是哪个区间
                Integer level = getLevel(rank, integers1);
                //获取等级
                o.put("level1",level);
            }
        });
        return mapList;*/
    }

    public Integer getLevel(Integer rank, List<Integer> levelRanks) {
        //排序
        levelRanks.sort(null);
        //
        for (int i = 0; i < levelRanks.size(); i++) {
            if (rank <= levelRanks.get(i)) {
                return i + 1;
            }
        }
        return levelRanks.size();
    }


    public void initMap(String type, Map<String, List<Integer>> v1, Map<String, List<Integer>> v2) {
        List<Integer> integers = v2.get(type);
        if (!CollectionUtils.isEmpty(integers)) {
            List<Integer> oldList = v1.get(type);
            if (CollectionUtils.isEmpty(oldList)) {
                oldList = new ArrayList<>();
            }
            oldList.addAll(integers);
            v1.put(type, oldList);
        }
    }


    public static List<String> getMonthsBetweenDates(String startTime, String endTime) {
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM");

        LocalDate startDate = LocalDate.parse(startTime + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate endDate = LocalDate.parse(endTime + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        List<String> months = new ArrayList<>();

        while (!startDate.isAfter(endDate)) {
            months.add(startDate.format(outputFormatter));
            startDate = startDate.plus(1, ChronoUnit.MONTHS);
        }

        return months;
    }
    public static void main(String[] args) {
        // 示例数据
        String startTime = "2024-02";
        String endTime = "2024-06";

        // 获取月份集合
        List<String> months = getMonthsBetweenDates(startTime, endTime);


    }
}
